import * as actionTypes from './actionTypes'
import axios from 'axios';


const changeList = (data) => ({
  type: actionTypes.CHANGE_LIST,
  data,
  totalPage: Math.ceil(data.length / 10)
})




export const searchFocus = () => ({
  type: actionTypes.SEARCH_FOCUS
})

export const searchBlur = () => ({
  type: actionTypes.SEARCH_BLUR
})

export const mouseEnter = () => ({
  type: actionTypes.MOUSE_ENTER
})

export const mouseLeave = () => ({
  type: actionTypes.MOUSE_LEAVE
})

export const changePage = (newPage) => ({
  type: actionTypes.CHANGE_PAGE,
  newPage
})

// 使用 redux-thunk 此时可以返回一个函数
export const getList = () => {
  return (dispatch) => {
    axios.get('/api/headerList.json').then((res) => {
      const resData = res.data
      if (resData.success) {
        dispatch(changeList(resData.data))
      }
    }).catch((e) => {
      console.log(e);
    })
  }
}
